layui.use('element', function () {
    var element = layui.element;
    $(".layui-nav-third-child").hide();
    $(".third-class").hover(function () {
        $(".layui-nav-third-child").hide();
        $(this).next().css('left', $(this).parent().parent().width() + 1);
        $(this).next().toggle();
    }, function () {
        $(".layui-nav-third-child").hide();
    });
    $(".layui-nav-third-child").hover(function () {
        $(this).toggle();
    }, function () {
        $(".layui-nav-third-child").hide();
    });
    $(".layui-nav-item").hover(function () {
        if ($(this).find('dl').css('right') == "0px") {
            $(this).find('dl').css('left', parseInt(parseInt(($(this).width()) - parseInt($(this).find('dl').width())) / 2));
            $(this).find('dl').css('right', "auto");
        }
    }, function () {
    });
    $("#boardSelector").on("click", function () {
        try {
            if ($(this).find('select')) {
                $("#boardSelector > div.layui-form-select dl").css('cssText', "left:" + parseInt(parseInt(($(this).width()) - parseInt($("#boardSelector > div.layui-form-select dl").width())) / 2) + " !important; right:auto !important;");
            }
        } catch (e) {
            console.log(e);
        }
    });
});

layui.use('layer', function () {
    var layer = layui.layer;
});

function new_file() {
    console.log(layer);

    console.log(layui.element);
    layer.confirm(MSG['confirm_newfile'], {
        title: false,
        btn: [MSG['newfile_yes'], MSG['newfile_no']]
        , btn2: function (index, layero) {
            layer.close(index);
        }
    }, function (index, layero) {
        mixlyjs.createFn();
        layer.close(index);
    });
}
function open_language() {
    function updateWorkspace() {
        let nowCode = editor.getValue();
        showTag();
        Code.initLanguage(false);
        renderContent();
        let layerDom = getid("layer_btn");
        if (MixlyBlockly?.layerOffsetLeft && layerDom?.offsetParent) {
            MixlyBlockly.layerOffsetLeft = layerDom.offsetParent.offsetLeft
                                         + layerDom.offsetParent.offsetWidth;
        }
        Blockly.fireUiEvent(window, 'resize');
        if (document.getElementById('changemod_btn') 
            && document.getElementById('changemod_btn').value === 0) {
            editor.setValue(nowCode, -1);
        } else {
            try {
                let xml = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
                Blockly.mainWorkspace.clear();
                Blockly.Xml.domToWorkspace(xml, Blockly.mainWorkspace);
                Blockly.mainWorkspace.scrollCenter();
            } catch (e) {
                //Blockly.mainWorkspace.clear();
                console.log(e);
            }
        }

        if (typeof serialInit !== "undefined") {
            serialInit();
        }
        if (typeof updateIndexText !== "undefined") {
            updateIndexText();
        }
        Blockly.hideChaff();
    }
    layer.confirm(MSG['choose_language'], {
        title: false,
        btn: ['简体中文', '繁体中文', 'English']
        , btn3: function (index, layero) {
            localStorage.Language = 'en';
            MixlyScriptLoader.loadLangJs(Code.LANG, "en", function() {
                updateWorkspace();
            });
            layer.closeAll();
        }
    }, function (index, layero) {
        localStorage.Language = 'zh-hans';
        MixlyScriptLoader.loadLangJs(Code.LANG, "zh-hans", function() {
            updateWorkspace();
        });
        layer.closeAll();
    }, function (index) {
        localStorage.Language = 'zh-hant';
        MixlyScriptLoader.loadLangJs(Code.LANG, "zh-hant", function() {
            updateWorkspace();
        });
        layer.closeAll();
    });
}
function open_theme() {
    layer.confirm(MSG['choose_theme'], {
        title: false,
        btn: ['Dark', 'Light']
        , btn2: function (index, layero) {
            localStorage.Theme = 'Light';
            Code.changeEditorTheme_light();
            $("body").css("background-color","#fff");
            $(".content").css("background-color","#fff");
            if (MixlyEnv.isClient) {
                nowWindow.setBackgroundColor("#fff");
            }
        }
    }, function (index, layero) {
        localStorage.Theme = 'Dark';
        Code.changeEditorTheme_dark();
        $("body").css("background-color","#181818");
        $(".content").css("background-color","#181818");
        if (MixlyEnv.isClient) {
            nowWindow.setBackgroundColor("#181818");
        }
        layer.close(index);
    });
}

function open_winsize() {
    layui.use(['layer', 'slider'], function () {
        let layer = layui.layer;
        let slider = layui.slider;

        //let winSize = electronwebContents.getZoomFactor() * 100;
        let winSize = MixlyEnv.winSize;
        let ins1 = null;

        layer.open({
            type: 1,
            id: "winsize",
            title: MSG['windowSize'],
            area: ['350px', '180px'],
            content: `<div style="padding:50px 20px 20px 20px;"><div id="slider_winsize" class="slider"></div></div>`,
            closeBtn: 1,
            btn: [indexText["复位"], indexText["应用"]],
            yes: function () {
                electronwebContents.setZoomFactor(MixlyEnv.winSize/100);
                MixlyEnv.winSize = 100;
                localStorage.winSize = 100;
                winSize = 100;
                electronwebContents.setZoomFactor(1.0);
                ins1.setValue(50);
                console.log("屏幕缩放比例为1.0");
                return false;
            },
            btn2: function () {
                MixlyEnv.winSize = winSize;
                localStorage.winSize = winSize;
                winSize = winSize / 100;
                electronwebContents.setZoomFactor(winSize);
                console.log("屏幕缩放比例为" + winSize);
                return false;
            },
            success: function (layero) {
                $(".layui-layer-page").css("z-index", "198910151");
                ins1 = slider.render({
                    elem: '#slider_winsize',
                    input: true, //输入框
                    min: 50, //最小值
                    max: 150, //最大值
                    value: winSize,
                    setTips: function(value){
                        return value + '%';
                    },
                    change: function (vals) {
                        vals = vals.replace("%", "");
                        winSize = vals-0;
                    }
                });
            },
            end: function () {
                $(".layui-layer-shade").remove();
            }
        });
    });
}

layui.use(['element', 'form'], function () {
    var element = layui.element;
    var form = layui.form;
    var layer = layui.layer;

    form.on('select(boards-type)', function (data) {
        try {
            if (profile[$("#boards-type").find("option:selected").text()]) {
                profile['default'] = profile[$("#boards-type").find("option:selected").text()];
            } else if (profile['Arduino/Genuino Uno'] != undefined) {
                profile['default'] = profile['Arduino/Genuino Uno'];
            }
        } catch (e) {
            console.log(e);
        }
        if (document.getElementById('changemod_btn').value != 0) {
            try {
                var xmlDom = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
                Blockly.mainWorkspace.clear();
                Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, xmlDom);
                renderContent();
            } catch (e) {
                console.log(e);
            }
        }
    });
});

//Demo
layui.use('form', function () {
    var form = layui.form;

    //监听提交
    form.on('submit(formDemo)', function (data) {
        layer.msg(JSON.stringify(data.field));
        return false;
    });
});